package com.placement.app.dao;

import java.math.BigDecimal;
import java.sql.SQLData;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

//import org.apache.commons.dbcp.BasicDataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.object.BatchSqlUpdate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;


import com.placement.app.model.ScreenUserBVO;
import com.placement.app.util.Constants;
import com.placement.app.util.JobUtil;
import com.placement.app.exception.JobRuntimeException;

public class UserDAOImpl  extends JdbcTemplate implements UserDAO {
	private static final Log logger = LogFactory.getLog(UserDAOImpl.class);

	// private TransactionM
	private DataSourceTransactionManager txManager;

	// Default Transaction Definition
	private DefaultTransactionDefinition defTransaction;
	
	public ScreenUserBVO getUser(ScreenUserBVO scrUserBVO)
	{		
		StoredProcHelperImpl storProcHelper = new StoredProcHelperImpl();
		
		String sql="select user_name,address,city,state,country,availability,special_consideration,ethinicity" +
		" from jobs.user where user_id="+scrUserBVO.getUserid();	
		System.out.println(sql);
		List list =   queryForList(sql);
		Iterator itr = list.iterator();
		while(itr.hasNext()){
			Map  map = (Map)itr.next();
			
			if(map.get("user_name")!=null){
				scrUserBVO.setUsername((String)map.get("user_name"));
			}							
			if(map.get("ADDRESS")!=null){				
				scrUserBVO.setAddress((String)map.get("ADDRESS"));
			}
			if(map.get("city")!=null){
				scrUserBVO.setCity((String)map.get("city"));
			}
			if(map.get("state")!=null){
				scrUserBVO.setState((String)map.get("state"));
			}
			if(map.get("country")!=null){
				scrUserBVO.setCountry((String)map.get("country"));
			}
			if(map.get("availability")!=null){
				scrUserBVO.setAvailability((String)map.get("availability"));
			}
			if(map.get("special_consideration")!=null){
					scrUserBVO.setSplcons((String)map.get("special_consideration"));
			}
			if(map.get("ethinicity")!=null){
				scrUserBVO.setEthinicity((String)map.get("ethinicity"));
			}
					
		}
				
		return scrUserBVO;
	}

	
	public void saveUser(ScreenUserBVO screenUserBVO)
	{
		
		/*
		String sql="update jobs.user set user_name='"+screenUserBVO.getUsername()+
		"',email_address='"+screenUserBVO.getEmail()+
		"',address='"+screenUserBVO.getAddress()+
		"',city='"+screenUserBVO.getCity()+
		"',state='"+screenUserBVO.getState()+
		"',country='"+screenUserBVO.getCountry()+
		"',availability='"+screenUserBVO.getAvailability()+
		"',special_consideration='"+screenUserBVO.getSplcons()+
		"',ethinicity='"+screenUserBVO.getEthinicity()+
		"' where user_id="+screenUserBVO.getUserid();		
		this.update(sql);
		*/
		DefaultTransactionDefinition txDef = this.getDefTransaction();
		TransactionStatus status = this.getTxManager().getTransaction(txDef);
		try{
		String sql="delete from jobs.user where user_id="+screenUserBVO.getUserid();
		this.update(sql);
		
		sql="insert into jobs.user(user_id,user_name,address, city,state,country,availability," +
			"special_consideration,ethinicity) values ("
		+screenUserBVO.getUserid()+",'"+screenUserBVO.getUsername()+"','"+screenUserBVO.getAddress()+"','"+screenUserBVO.getCity()
		+"','"+screenUserBVO.getState()+"','"+screenUserBVO.getCountry()+"','"+screenUserBVO.getAvailability()
		+"','"+screenUserBVO.getSplcons()+"','"+screenUserBVO.getEthinicity()+"')";
		System.out.println(sql);
		this.update(sql);		
		this.getTxManager().commit(status);
	} catch (RuntimeException e) {
		this.getTxManager().rollback(status);
		logger.error(">>Save User>>",e );
		throw new JobRuntimeException(e);
	}
	}

	
	public void registerUser(ScreenUserBVO screenUserBVO)
	{			
		DefaultTransactionDefinition txDef = this.getDefTransaction();
		TransactionStatus status = this.getTxManager().getTransaction(txDef);
		try{
		String sql="insert into jobs.user_login(email_address, password, confirm_password, login_date) values ('"+screenUserBVO.getEmail()+
		"','" + screenUserBVO.getPassword()+"','" +screenUserBVO.getConfirmPassword()+"', now())";	
		this.update(sql);
		this.getTxManager().commit(status);
		} catch (RuntimeException e) {
			this.getTxManager().rollback(status);
			logger.error(">>Save User Login>>",e );
			throw new JobRuntimeException(e);
		}
	}
	
	public boolean checkUserExist(ScreenUserBVO scrUserBVO){
		
		String sql="select user_id" +
		" from jobs.user_login where email_address='"+scrUserBVO.getEmail()+"'";	
		System.out.println(sql);
		List list =   queryForList(sql);
		Iterator itr = list.iterator();
		while(itr.hasNext()){
			Map  map = (Map)itr.next();
			if(map.get("user_id")!=null){				
				return true;
			}		
			
		}
		return false;
	}
	
	public ScreenUserBVO findUserId(ScreenUserBVO scrUserBVO){
		
		String sql="select user_id" +
		" from jobs.user_login where password='"+scrUserBVO.getPassword()+"' and email_address='"+scrUserBVO.getEmail()+"'";	
		System.out.println(sql);
		List list =   queryForList(sql);
		Iterator itr = list.iterator();
		while(itr.hasNext()){
			Map  map = (Map)itr.next();
			if(map.get("user_id")!=null){
				String strUserid=Integer.toString((Integer)map.get("user_id"));
				System.out.println(strUserid);
				scrUserBVO.setUserid(strUserid);	
			}		
		}
		return scrUserBVO;
	}
		
	public DefaultTransactionDefinition getDefTransaction() {
		return defTransaction;
	}


	public void setDefTransaction(DefaultTransactionDefinition defTransaction) {
		this.defTransaction = defTransaction;
	}


	public DataSourceTransactionManager getTxManager() {
		return txManager;
	}


	public void setTxManager(DataSourceTransactionManager txManager) {
		this.txManager = txManager;
	}
	
}
